import Vue from 'vue'
import Router from 'vue-router'
import cityRouters from './cityRouters'
import distRouters from './distRouters'
import schoolRouters from './schoolRouters'
import statsRouters from './statsRouters'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: [{
    path: '/login',
    name: 'login',
    component: () => import('@/views/login.vue')
  }].concat(cityRouters, distRouters, schoolRouters,statsRouters)
})

router.beforeEach((to, from, next) => {
  let obj = sessionStorage.getItem('userData') === 'undefined' ? '' : JSON.parse(sessionStorage.getItem('userData'))
  if (obj) {
    if (to.fullPath === '/') {
      // next()
      let _roleKey = obj.roleKey
      if (_roleKey === 'mark_teacher') {
        next(`/startEvaluation`)
      } else if (_roleKey === 'school') {
        next(`/schoolStudentInfo`)
      } else if (_roleKey === 'city') {
        next(`/examPlan`)
      } else {
        next(`/infoManagement`)
      }
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
